package com.sztz.oss.minio;

import com.sztz.oss.OssConfig;
import io.minio.MinioClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Minio配置类，用来生成Minio客户端与Minio模板
 * @author yzy
 * @date 2022/9/30 14:22
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({MinioClient.class})
@EnableConfigurationProperties(OssConfig.class)
@ConditionalOnProperty(value = "oss.type", havingValue = "minio")
public class MinioConfiguration {

    /**
     * 生成Minio客户端
     * @author yzy
     * @date 2022/9/30 14:22
     */
    @Bean
    @ConditionalOnMissingBean(MinioClient.class)
    public MinioClient minioClient(OssConfig ossConfig){
        return MinioClient.builder()
                .endpoint(ossConfig.getEndpoint())
                .credentials(ossConfig.getAccessKey(),ossConfig.getSecretKey())
                .build();
    }
    /**
     * 生成Minio Api模板
     * @author yzy
     * @date 2022/9/30 14:22
     */
    @Bean
    @ConditionalOnBean(MinioClient.class)
    @ConditionalOnMissingBean(MinioTemplate.class)
    public MinioTemplate minioTemplate(MinioClient minioClient,OssConfig ossConfig){
        return new MinioTemplate(minioClient,ossConfig);
    }
}
